<?php

/**
 *  diy套装效果图重新生成或保存
 */
function test_commit_diy_saveimg($json)
{
	$id = get_jsonValue($json,"id") ;
	$img = new set_image() ;
	$ok = $img->createAndSave_Diy($id) ;
	
	$ret = array() ;
	if ($ok == true)	$ret['ok'] = 1 ;
	else 				$ret['ok'] = 0 ;
	
	$test = new test_bk() ;
	$ret['value'] = $test->get_table_for_page(1 , 1 , false , "" , "diy" , " where `id` = '$id'") ;	
	return $ret ;
}

// 获取所有diy模板记录
function test_diy_list($json)
{
	$filter_name = trim(get_jsonValue($json,"filter_name")) ;
	$test = new test_bk() ;
	$ret = $test->get_table_for_page(1 , 1000 , false , $filter_name , "diy") ;
	return $ret ;	
}
function test_mydiy_list($json)
{
	$filter_name = trim(get_jsonValue($json,"filter_name")) ;
	$userid		 = intval(get_jsonValue($json,"userid")) ;
	$test = new test_bk() ;
	$ret = $test->get_table_for_page(1 , 1000 , false , $filter_name , "user_diy" , " where `user_id` = '$userid'") ;
	return $ret ;	
}

// 添加新的diy模板
function test_add_diy($json)
{
	$nm = get_jsonValue($json,"name") ;
	$tips = get_jsonValue($json,"tips") ;
	$app_return =  get_jsonValue($json,"app_return") ;
	$space_id =  get_jsonValue($json,"space_id") ;
	
	$new_id = sql_insert("INSERT INTO `diy` (`name`, `tips`,`app_return`,`space_id`) VALUES ('$nm','$tips', '$app_return','$space_id')") ;
	$style_arr = get_jsonValue($json,"style_arr") ;
	foreach ($style_arr as $sy){
		sql_insert("INSERT INTO `diy_style` (`diy_id` , `style_id`) values ('$new_id' , '$sy') ON DUPLICATE KEY UPDATE `style_id` = '$sy'") ;
	}
	$color_arr = get_jsonValue($json,"color_arr") ;
	foreach ($color_arr as $sy){
		sql_insert("INSERT INTO `diy_color` (`diy_id` , `color_id`) values ('$new_id' , '$sy') ON DUPLICATE KEY UPDATE `color_id` = '$sy'") ;
	}
	
	$ret = array() ;
	$ret['ok'] = 1 ;
	return $ret ; 
}

// 修改diy模板数据
function test_edit_diy($json)
{
	$id = get_jsonValue($json,"id") ;
	$nm = get_jsonValue($json,"name") ;
	$tips = get_jsonValue($json,"tips") ;
	$app_return =  get_jsonValue($json,"app_return") ;
	$space_id =  get_jsonValue($json,"space_id") ;

	sql_query("update `diy` set `name` = '$nm', `tips` = '$tips',`app_return` = '$app_return' , `space_id` = '$space_id' where `id` = '$id'") ;
	sql_query("delete from `diy_style` where `diy_id` = '$id'") ;
	sql_query("delete from `diy_color` where `diy_id` = '$id'") ;
	
	$style_arr = get_jsonValue($json,"style_arr") ;
	foreach ($style_arr as $sy){
		sql_insert("INSERT INTO `diy_style` (`diy_id` , `style_id`) values ('$id' , '$sy') ON DUPLICATE KEY UPDATE `style_id` = '$sy'") ;
	}
	$color_arr = get_jsonValue($json,"color_arr") ;
	foreach ($color_arr as $sy){
		sql_insert("INSERT INTO `diy_color` (`diy_id` , `color_id`) values ('$id' , '$sy') ON DUPLICATE KEY UPDATE `color_id` = '$sy'") ;
	}
	
	$ret = array() ;
	$ret['ok'] = 1 ;
	return $ret ; 
}

/**
 * 删除一个DIY套装模板记录 , 位置什么的都删除了.
 */
function test_delete_from_diy($json)
{
	$id 			= get_jsonValue($json,"id") ;
	if (sql_check("select `id` from `diy` where `id` = '$id'")){
		sql_query("delete from `diy` where `id` = '$id'") ;
		sql_query("delete from `diy_pos` where `diy_id` = '$id'") ;
	}
	$ret = array() ;
	$ret['result'] = sql_check("select `id` from `diy` where `id` = '$id'") ? 0 : 1 ;
	$ret['del_id'] = $id ;
	return $ret ;
}
/**
 * 获取diy的详细组合数据.
 *
 * @param 条件
 * @return 结果列表
 */
function test_get_diy_pos_for_where($where)
{
	$result = sql_fetch_rows("select * from diy_pos where $where order by `pos` asc");
	$test = new test_bk() ;
	foreach ($result as &$t){
		$val = $test->get_diy_pos_for_ts_image($t['id']) ;
		$t['img_id'] = $val[0] ;
		$t['url'] = $val[1] ;
		$t['class_id'] = $val[2] ;
		$t['name'] = ($t['show_name'] == "")?$val[3]:$t['show_name'] ;
		$ct = intval(sql_fetch_one_cell("select `count` from `diy_item_count` where `pos_id` = '{$t['id']}' and `item_id` = '{$t['item_id']}'")) ;		
		$t['count'] = ($ct >= 1) ? $ct : 1 ;
		
		$one = sql_fetch_one("select * from diy_pos_ex where `pos_id` = '{$t['id']}' and `item_id` = {$t['item_id']}") ;
		if (is_array($one) && array_key_exists("anchor_x", $one)){
			$t['x']			= floatval($one['x']) ;			$t['y']			= floatval($one['y'])  ;
			$t['anchor_x']	= floatval($one['anchor_x'])  ;	$t['anchor_y']	= floatval($one['anchor_y']) ;
			$t['zoom']		= floatval($one['zoom']) ;
			$t['mirror']	= intval($one['mirror']) ;
		}
	}
	return $result ;
}
function test_taozhuang_diy_exparam($json)
{
	$tzid = get_jsonValue($json,"tzid") ;
	$ret = array() ;
	$ret['color'] = sql_fetch_rows("select * from diy_color where diy_id = '$tzid'") ;
	$ret['style'] = sql_fetch_rows("select * from diy_style where diy_id = '$tzid'") ;
	return $ret ;
}

/**
 * 通过diy ID 获取diy列表   
 */
function test_get_diy_ForID($json)
{
	$id = intval(get_jsonValue($json , "id")) ;
	
	$ret = array() ;
	$ret['diy_pos'] = test_get_diy_pos_for_where("`diy_id` = '$id'") ; 
	
	$img_bk = intval(sql_fetch_one_cell("select `back_img` from `diy` where `id` = '$id'")) ;
	$ret['img_bk_id'] = $img_bk ;
	$ret['img_bk'] = array() ;
	if ($img_bk > 0){		
		util_set_IMAGE_one($ret['img_bk'] , $img_bk) ;		
	}
	return $ret ;	
}
function test_getmy_diy_ForID($json)
{
	$id = intval(get_jsonValue($json , "id")) ;
	
	$result = sql_fetch_rows("select ut.* , t.`pos`,t.`father`,t.`x`,t.`y`,t.`anchor_x`,t.`anchor_y`,t.`rotate`,t.`zoom`,t.`mirror` from user_diy_pos ut inner join diy_pos t on ut.`pos_id` = t.`id` where ut.`user_diy_id` = '$id' order by t.`pos` asc");
	$test = new test_bk() ;
	foreach ($result as &$t){
		$val = $test->get_diy_pos_for_ts_image($t['id'] , 2) ;
		$t['img_id'] = $val[0] ;
		$t['url'] = $val[1] ;
		$t['class_id'] = $val[2] ;
		$t['name'] = ($t['show_name'] == "")?$val[3]:$t['show_name'] ;
	}
	$img_bk = intval(sql_fetch_one_cell("select d.`back_img` from `user_diy` ud inner join `diy` d on ud.`diy_id` = d.`id` where ud.`id` = '$id'")) ;
	$ret['img_bk_id'] = $img_bk ;
	$ret['img_bk'] = array() ;
	if ($img_bk > 0){		
		util_set_IMAGE_one($ret['img_bk'] , $img_bk) ;		
	}
	
	$ret = array() ;
	$ret['diy_pos'] = $result ;
	return $ret ;	
}
/**
 * 获取diy可以使用的单品列表,包括图片地址等 * 空间也要联合判定下.
 */
function test_diy_getuseing_itemlist($json)
{
	$classid = intval(get_jsonValue($json , "id")) ;
	$spaceid = intval(get_jsonValue($json , "spaceid")) ;
	$pos_id = intval(get_jsonValue($json, "pos_id")) ;
	$filter_name = trim(get_jsonValue($json,"filter_name")) ;
	
	$cla_list = trim(sql_fetch_one_cell("select grp_list from diy_class_grp where class_id = '$classid'")) ;
	$class_str_list = "$classid" ;
	if (strlen($cla_list) > 0)	$class_str_list .= " , $cla_list" ;
	
	$where = "" ;
	if (strlen($filter_name) > 0){
		$where = " and i.`name` like '%$filter_name%'" ;
	}
	
	util_get_PAGINATION($json , $offset , $limit) ;
	$start = ($offset - 1) * $limit ;	
	temp_check_and_createtable__mem_diy_tz_item() ;
	
	$allcount = sql_fetch_one_cell("select count(i.`id`) from `sys_item` i inner join mem_diy_tz_item m on i.`id` = m.`item_id` inner join `sys_item_space` s on i.`id` = s.`item_id` and s.`space_id` = '$spaceid' and i.`app_return` <= 1 where i.`class_id` in ($class_str_list) $where order by i.`top_time` desc , i.`update_at` desc") ;
	
	$ret = array() ;
	$ret['pagination'] = array() ;
	util_set_PAGINATION($ret['pagination'] , $allcount , $limit , $offset) ;
	
	$bitem = sql_fetch_rows("select i.`id` , i.`name` , i.`class_id` , pp.`x` , pp.`y` , pp.`anchor_x` , pp.`anchor_y` , pp.`zoom` , ex.x as ex_x , ex.y as ex_y , ex.anchor_x as ex_anchor_x , ex.anchor_y as ex_anchor_y , ex.zoom as ex_zoom from `sys_item` i join mem_diy_tz_item m on i.`id` = m.`item_id` inner join `sys_item_space` s on i.`id` = s.`item_id` and s.`space_id` = '$spaceid' and i.`app_return` <= 1 left join diy_pos_ex ex on ex.pos_id = '$pos_id' and ex.item_id = i.`id` left join diy_pos pp on pp.`id` = '$pos_id' where i.`class_id` in ($class_str_list) $where order by `top_time` desc , `update_at` desc limit $start , $limit") ;
	
	$test = new test_bk() ;
	foreach ($bitem as &$b){		
		$slt = $test->get_item_default_ts_image($b['id']) ;
		$b['image_id'] = $slt[0] ;
		$b['url'] = $slt[1] ;
		$ct = intval(sql_fetch_one_cell("select `count` from `diy_item_count` where `pos_id` = '$pos_id' and `item_id` = '{$b['id']}'")) ;		
		$b['count'] = ($ct >= 1) ? $ct : 1 ;
	}
	$ret['list'] = $bitem ;
	return $ret ;
}
function test_diy_edit_item_pos_ex($json)
{
	$pos_id = intval(get_jsonValue($json, "pos_id")) ;
	$item_id = intval(get_jsonValue($json, "item_id")) ;
	
	$ex_x 			= intval(get_jsonValue($json, "ex_x")) ;
	$ex_y 			= intval(get_jsonValue($json, "ex_y")) ;
	$ex_anchor_x 	= doubleval(get_jsonValue($json, "ex_anchor_x")) ;
	$ex_anchor_y 	= doubleval(get_jsonValue($json, "ex_anchor_y")) ;
	$ex_zoom 		= doubleval(get_jsonValue($json, "ex_zoom")) ;
	
	sql_insert("INSERT INTO `diy_pos_ex` (`pos_id`, `item_id`, `x`, `y`, `anchor_x`, `anchor_y`, `rotate`, `zoom`, `mirror`) VALUES ('$pos_id', '$item_id', '$ex_x', '$ex_y', '$ex_anchor_x', '$ex_anchor_y', '0', '$ex_zoom', '0') on duplicate key update `x` = '$ex_x' , `y` = '$ex_y' , `anchor_x` = '$ex_anchor_x' , `anchor_y` = '$ex_anchor_y' , `zoom` = '$ex_zoom'") ;
	
	$ret = array() ;
	$ret['data'] = sql_fetch_one("select * from `diy_pos_ex` where `pos_id` = '$pos_id' and `item_id` = '$item_id'") ;
	return $ret ; 
}
function test_diy_del_item_pos_ex($json)
{
    $pos_id = intval(get_jsonValue($json, "pos_id")) ;
    $item_id = intval(get_jsonValue($json, "item_id")) ;

    sql_query("delete from `diy_pos_ex` where `pos_id` = '$pos_id' and `item_id` = '$item_id'") ;
    $ret = array() ;
    $ret['pos_id'] = $pos_id ;
    $ret['item_id'] = $item_id ;
    $ret['message'] = "ok" ;
    return $ret ;
}
// 在diy模板中插入新的商品
function test_diy_insert_newitem($json)
{
	$diyid = get_jsonValue($json,"id") ;
	$itemid = get_jsonValue($json,"itemid") ;
	$imgid = get_jsonValue($json , "imgid") ;
	
	$maxpos = intval(sql_fetch_one_cell("select max(`pos`) from diy_pos where `diy_id` = '$diyid'")) ;
	$maxpos += 100 ;

	$newid = sql_insert("INSERT INTO `diy_pos` (`diy_id`, `item_id`,`pos`,`x`,`y`,`img_id`) VALUES ('$diyid','$itemid', '$maxpos','480','360','$imgid')") ;		
	
	$ret = array() ;
	$ret['new_list'] = test_get_diy_pos_for_where("`id` = $newid") ; 
	return $ret ;	
}

// 更新pos中的字段
function test_diy_update_base_pos($json)
{
	$id = get_jsonValue($json,"id") ;
	$feild = get_jsonValue($json,"field") ;
	$item_id = intval(get_jsonValue($json,"item_id")) ;

	$update = "" ;
	foreach ($feild as $f)
	{
		$k = $f[0] ;
		$v = $f[1] ;
		if ($update == "")	$update = " `$k` = '$v' " ;
		else 				$update .= " , `$k` = '$v' " ;
	}

	$is_ex = 0 ;
    if ($item_id == -999){
        sql_query("update `diy_pos` set $update where `id` = '$id'");
    }else {
        $one = sql_fetch_one("select * from diy_pos_ex where `pos_id` = '$id' and `item_id` = '$item_id'");
        if (is_array($one) && array_key_exists("anchor_x", $one)) {
            sql_query("update `diy_pos_ex` set $update where `pos_id` = '$id' and `item_id` = '$item_id'");
            $is_ex = 1 ;
        } else {
            sql_query("update `diy_pos` set $update where `id` = '$id'");
        }
    }
	
	$ret = array() ;
	$ret['result'] = 1 ;
	$ret['ex'] = $is_ex ;
	$ret['dt'] = array() ;
	if ($is_ex == 1){
        $one = sql_fetch_one("select * from diy_pos_ex where `pos_id` = '$id' and `item_id` = '$item_id'");
        if (is_array($one) && array_key_exists("anchor_x", $one)){
            $ret['dt']['item_id']	= intval($one['item_id']) ;
            $ret['dt']['x']			= floatval($one['x']) ;
            $ret['dt']['y']			= floatval($one['y'])  ;
            $ret['dt']['anchor_x']	= floatval($one['anchor_x'])  ;
            $ret['dt']['anchor_y']	= floatval($one['anchor_y']) ;
            $ret['dt']['zoom']		= floatval($one['zoom']) ;
            $ret['dt']['mirror']	= intval($one['mirror']) ;
        }
    }
	return $ret ; 
}
function test_diy_update_item_count($json)
{
	$id = get_jsonValue($json,"id") ;
	$itemid = get_jsonValue($json,"item_id") ;
	$count = get_jsonValue($json,"count") ;
	if ($count  < 1){
		$ret = array() ;
		$ret['result'] = 0 ;
		return $ret ; 
	}
	sql_insert("INSERT INTO `diy_item_count` (`pos_id`, `item_id`, `count`) VALUES ('$id', '$itemid', '$count') on duplicate key update `count` = '$count'") ;

	$ret = array() ;
	$ret['result'] = 1 ;
	return $ret ; 
}
// 删除一个diy贴图
function test_diy_delete_pos($json)
{
	$id = get_jsonValue($json,"id") ;	
	$one = sql_fetch_one("select `pos` , `diy_id` from `diy_pos` where `id` = '$id'") ;
	
	$pos = intval($one['pos']) ;
	$did = intval($one['diy_id']) ;
	
	$ret = array() ;
	if (intval(sql_fetch_one_cell("select count(`id`) from `diy_pos` where `diy_id` = '$did' and `father` = '$pos'")) > 0){
		$ret['result'] = 0 ;	// err
		$ret['id'] = $id ;
		return $ret ;
	}
	
	sql_query("delete from `diy_pos` where `id` = '$id'") ;
	
	
	if (intval(sql_fetch_one_cell("select `id` from `diy_pos` where `id` = '$id'")) == $id){
		$ret['result'] = 0 ;
	}else{
		$ret['result'] = 1 ;
	}
	$ret['id'] = $id ;
	return $ret ;
}
// 层数的移动
function test_diy_pos_move($json)
{
	$id = get_jsonValue($json,"id") ;
	$type = get_jsonValue($json,"type") ;
	$one = sql_fetch_one("select `pos` , `diy_id` , `father` from `diy_pos` where `id` = '$id'") ;
	
	$pos = intval($one['pos']) ;
	$did = intval($one['diy_id']) ;
	$ftr = intval($one['father']) ;
	
	if ($type > 0){
		$two = sql_fetch_one("select `id` , `pos` , `diy_id` from `diy_pos` where `diy_id` = '$did' and `father` = '$ftr' and `pos` > '$pos' order by `pos` asc") ;
	}else{
		$two = sql_fetch_one("select `id` , `pos` , `diy_id` from `diy_pos` where `diy_id` = '$did' and `father` = '$ftr' and `pos` < '$pos' order by `pos` desc") ;
	}
	
	$ret = array() ;
	if (is_array($two) && array_key_exists('pos', $two)){
		$two_id = intval($two['id']) ;
		$two_pos = intval($two['pos']) ;
		$two_did = intval($two['diy_id']) ;
		
		sql_query("update `diy_pos` set `pos` = -1 where `id` = '$id'") ;
		sql_query("update `diy_pos` set `pos` = '$pos' 		where `id` = '$two_id'") ;
		sql_query("update `diy_pos` set `pos` = '$two_pos' 	where `id` = '$id'") ;
		
		$ret['result'] = 1 ;
		$ret['change1_id'] = $id ;
		$ret['change1_val'] = $two_pos ;
		$ret['change2_id'] = $two_id ;
		$ret['change2_val'] = $pos ;
	}else{
		$ret['result'] = 0 ;
		$ret['message'] = "没有移动的必要" ;
	}
	return $ret ;
}

/**
 *  获取DIY pos表中某个记录对应的单品详细信息
 */
function test_getDIY_for_POS_ITEM($json)
{
	$item_id = intval(get_jsonValue($json , "id")) ;
	$posid = get_jsonValue($json , "posid") ;	
	
	$ret = array() ;
	$ret['preview'] = array() ;
	
	if ($item_id == 0 || $posid == 0){
		return $ret ;
	}

	$test = new test_bk() ;
	$default = $test->get_diy_pos_for_ts_image($posid) ;
	$defaultimg = $default[0] ;
	$defaultpos = sql_fetch_one_cell("select `pos` from diy_pos where `id` = '$posid'") ;
	
	$item = sql_fetch_one("select * from `sys_item` where `id` = '$item_id'") ;
	$vals = sql_fetch_array("select p.* , f.`name` as f_name , m.`name` as m_name from base_sys_preview p left join base_sys_fabric f on p.fid = f.id left join base_sys_material m on p.mid = m.id where `item_id` = '$item_id'" , "id") ;	

	foreach ($vals as $v)
	{
		$a = array() ;
		util_set_ITEM_PREVIEW($a , $v , $item) ;
		$a['id'] = $v['id'] ;
		$a['img_tz_id'] = $v['img_tz_id'] ;
		$a['pos'] = $defaultpos ;
		$a['f_name'] = is_null($v['f_name'])?"":$v['f_name'] ;
		$a['m_name'] = is_null($v['m_name'])?"":$v['m_name'] ;
		$a['is_default'] = ($v['img_tz_id'] == $defaultimg) ? 1 : 0 ;
		$ret['preview'][] = $a ;
	}
	if (count($ret['preview']) == 0){
		$a = array() ;
		$a['id'] = $item_id ;
		$a['img_tz_id'] = $defaultimg ;
		$a['pos'] = $defaultpos ;
		$a['f_name'] = "" ;
		$a['m_name'] = "" ;
		$a['is_default'] = 1 ;

		$a['set_image'] = array() ;
		$a['fabric'] 	= "未设置单品透视图" ;
		$a['material'] 	= "" ;
		
		util_set_IMAGE_one($a['set_image'] , $defaultimg) ;
		$ret['preview'][] = $a ;
	}
	
	return $ret ;
}
/**
 *  某个DIY套装另存为
 */
function test_diy_saveAs($json)
{
	$ret = array() ;
	$id 			= get_jsonValue($json,"id") ;
	
	$one = sql_fetch_one("select * from `diy` where `id` = '$id'") ;
	if (is_null($one) == true || is_array($one) == false){
		$ret['result'] = 0 ;
		$ret['memo'] = "没有找到此DIY套装" ;
		return $ret ;
	}
	$newid = sql_insert("INSERT INTO `diy` (`name`, `tips`,`app_return`,`space_id`,`img_small_id`,`img_thumb_id`,`img_url_id`) VALUES ('{$one['name']} 副本','{$one['tips']}', '{$one['app_return']}', '{$one['space_id']}', '0', '0', '0')") ;
	$onelist = sql_fetch_rows("select * from `diy_pos` where `diy_id` = '$id'") ;
	foreach ($onelist as $o){
		sql_insert("INSERT INTO `diy_pos` (`diy_id`, `item_id` , `img_id`,`pos`,`x`,`y`,`father`,`anchor_x`,`anchor_y`,`rotate`,`zoom`,`mirror`) VALUES ('$newid','{$o['item_id']}' , '{$o['img_id']}', '{$o['pos']}' ,'{$o['x']}','{$o['y']}','{$o['father']}','{$o['anchor_x']}','{$o['anchor_y']}','{$o['rotate']}','{$o['zoom']}','{$o['mirror']}')") ;
	}
	
	$ret = array() ;
	$ret['result'] = 1 ;
	$ret['newid'] = $newid ;
	return $ret ;
}
/**
 * 某个DIY套装编辑另存为默认显示的透视图
 */
function test_diy_saveDefault($json)
{
	$ret = array() ;
	$did 			= get_jsonValue($json,"id") ;
	if (sql_check("select `id` from `diy` where `id` = '$did'") == false){
		$ret['result'] = 0 ;
		$ret['memo'] = "失败没有找到此DIY套装" ;
		return $ret ;
	}
	$def_ids = get_jsonValue($json,"arr") ;
	foreach ($def_ids as $d)
	{
		$defid = get_jsonValue($d , 'id') ;
		$defimg = get_jsonValue($d , 'img_id') ;
		$defitem= get_jsonValue($d , "item_id") ;
		if (intval($defimg) > 0){
			sql_query("update `diy_pos` set `img_id` = '$defimg' , `item_id` = '$defitem' where `id` = '$defid'") ;
		}
	}
	
	$ret = array() ;
	$ret['result'] = 1 ;
	$ret['memo'] = "保存成功" ;
	return $ret;	
}


function test_diy_set_bakimg($json)
{
	$id = intval(get_jsonValue($json , "id")) ;
	$bkimg= intval(get_jsonValue($json , "imgid")) ;
	
	sql_query("update `diy` set `back_img` = '$bkimg' where `id` = '$id'") ;		
	
	$ret = array() ;
	
	$newid = intval(sql_fetch_one_cell("select `back_img` from `diy` where `id` = '$id'")) ;
	
	if ($newid == $bkimg){
		$ret['result'] = 1 ;
		$ret['imgid'] = $bkimg ;
		$ret['message'] = "套装底图更新成功" ;
	}else{
		$ret['result'] = -1 ;
		$ret['message'] = "更改错误,请联系管理员" ;
	}
	
	return $ret ;
}

?>